package com.iamteer.listener;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.iamteer.entity.Admin;

/**
 * 初始化在线列表集合
 * @author Chuck
 *
 */
public class OnlineAdminListener implements ServletContextListener {

	// 1. ServletContext 对象创建
	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		// 创建集合：存放在线用户
		// 每次当用户登录后，就往这个集合中添加当前登陆者
		List<Admin> onlineList = new ArrayList<Admin>();
		// 放入 ServletContext 中
		arg0.getServletContext().setAttribute("onlineList", onlineList);
	}
	
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		ServletContext sc = arg0.getServletContext();
		Object obj = sc.getAttribute("onlineList");
		if (obj != null) {
			sc.removeAttribute("onlineList");
		}
	}
}
